﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmManageAccount : Form
    {
        Form opener;
        Person account;
        List<Department> departments;
        List<PayGrade> payGrades;
        List<Person> managers;
        List<Title> titles;
        List<Skill> skills;

        public frmManageAccount(Form Opener)
        {
            opener = Opener;
            account = new Person();
            account.Username = "";
            account.Password = "";
            InitializeComponent();
        }

        public frmManageAccount(Form Opener, Person Account)
        {
            opener = Opener;
            account = Account;
            InitializeComponent();
        }

        public void refresh()
        {
            payGrades = PayGrade.getAllPayGrades();
            cmbPayGrade.Items.Clear();
            foreach (PayGrade item in payGrades)
            {
                cmbPayGrade.Items.Add(item.Name);
            }


            departments = Department.loadAllDepartments();
            cmbDepartment.Items.Clear();
            foreach (Department item in departments)
            {
                cmbDepartment.Items.Add(item.Name);
            }

            managers = Person.loadAllActiveNurses();
            for (int i = managers.Count - 1; i >= 0; i--)
            {
                if (!managers[i].IsManager)
                {
                    managers.RemoveAt(i);
                }
            }
            cmbManagedBy.Items.Clear();
            foreach (Person item in managers)
            {
                cmbManagedBy.Items.Add(item.FullName);
            }

            titles = Title.getAllTitles();
            cmbTitle.Items.Clear();
            foreach (Title item in titles)
            {
                cmbTitle.Items.Add(item.Name);
            }

            skills = Skill.getAllSkills();
            clbSkills.Items.Clear();
            foreach (Skill item in skills)
            {
                clbSkills.Items.Add(item.Name);
            }

            if (account != null)
            {
                foreach (int item in account.SkillTableIDs)
                {
                    int i = clbSkills.Items.IndexOf(item);

                    if (i > 0)
                    {
                        clbSkills.SetItemChecked(i, true);
                    }
                }

                txtFirstName.Text = account.FirstName;
                txtLastName.Text = account.LastName;
                txtUserName.Text = account.Username;
                txtPassword.Text = account.Password;
                txtHomePhoneNumber.Text = account.HomePhone;
                txtCellPhoneNumber.Text = account.CellPhone;
                nudHours.Value = account.ApproxHours;
                cmbPayGrade.Text = PayGrade.loadData(account.PayGradeTableID).Name;
                if (account.DepartmentTableIDs.Count > 0)
                {
                    foreach (Department department in departments)
                    {
                        if (account.DepartmentTableIDs[0] == department.DepartmentID)
                            cmbDepartment.Text = department.Name;
                    }
                }
                if (account.TitleTableIDs.Count > 0)
                {
                    foreach (Title title in titles)
                    {
                        if (account.TitleTableIDs[0] == title.TitleID)
                            cmbTitle.Text = title.Name;
                    }
                }
                cmbManagedBy.Text = Person.loadPerson(account.SuperiorID).FullName;
                chkManager.Checked = account.IsManager;
                chkActive.Checked = account.Active;
            }
        }

        private void btnEditPayGrades_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmPayGrades payGrades = new frmPayGrades(this);
            payGrades.Show();
        }

        private void btnEditDepartments_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmDepartments departments = new frmDepartments(this);
            departments.Show();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtFirstName.Text == "")
            {
                MessageBox.Show("Please enter a first name.");
                txtFirstName.Focus();
            }
            else if (txtLastName.Text == "")
            {
                MessageBox.Show("Please enter a last name.");
                txtLastName.Focus();
            }
            else if (txtUserName.Text == "")
            {
                MessageBox.Show("Please enter a user name.");
                txtUserName.Focus();
            }
            else if (txtPassword.Text == "")
            {
                MessageBox.Show("Please enter a password.");
                txtPassword.Focus();
            }
            else if (cmbDepartment.Text == "")
            {
                MessageBox.Show("Please select a department.");
                cmbDepartment.Focus();
            }
            else if (cmbPayGrade.Text == "")
            {
                MessageBox.Show("Please select a paygrade.");
                cmbPayGrade.Focus();
            }
            else if (cmbManagedBy.Text == "")
            {
                MessageBox.Show("Please select a manager.");
                cmbManagedBy.Focus();
            }
            else if (cmbTitle.Text == "")
            {
                MessageBox.Show("Please select a title.");
                cmbDepartment.Focus();
            }
            else if (txtHomePhoneNumber.Text.Length != 12)
            {
                MessageBox.Show("Please type in a complete home phone number");
                txtHomePhoneNumber.Focus();
            }
            else if (txtCellPhoneNumber.Text.Length != 12)
            {
                MessageBox.Show("Please type in a complete cell phone number");
                txtCellPhoneNumber.Focus();
            }
            else
            {
                account.FirstName = txtFirstName.Text;
                account.LastName = txtLastName.Text;
                account.Username = txtUserName.Text;
                account.ApproxHours = (int)nudHours.Value;
                account.HomePhone = txtHomePhoneNumber.Text;
                account.CellPhone = txtCellPhoneNumber.Text;
                account.Address = rtbAddress.Text.ToString();
                account.Password = txtPassword.Text;

                foreach (PayGrade item in payGrades)
                {
                    if (item.Name == cmbPayGrade.Text)
                    {
                        account.PayGradeTableID = item.PayGradeID;
                        break;
                    }
                }

                account.DepartmentTableIDs.Clear();

                foreach (Department item in departments)
                {
                    if (item.Name == cmbDepartment.Text)
                    {
                        account.DepartmentTableIDs.Add(item.DepartmentID);
                        break;
                    }
                }

                foreach (Person item in managers)
                {
                    if (item.FullName == cmbManagedBy.Text)
                    {
                        account.SuperiorID = item.PERID;
                        break;
                    }
                }

                account.TitleTableIDs.Clear();

                foreach (Title item in titles)
                {
                    if (item.Name == cmbTitle.Text)
                    {
                        account.TitleTableIDs.Add(item.TitleID);
                        break;
                    }
                }

                account.SkillTableIDs.Clear();
                foreach (object item in clbSkills.CheckedItems)
                {
                    string name = item.ToString();
                    foreach (Skill skill in skills)
                    {
                        if (skill.Name == name)
                        {
                            account.SkillTableIDs.Add(skill.SkillID);
                            break;
                        }
                    }
                }

                account.IsManager = chkManager.Checked;
                account.Active = chkActive.Checked;

                int exitResult = account.saveData();
                if (exitResult < 0)
                {
                    switch (exitResult)
                    {
                        case (-1):
                            {
                                MessageBox.Show("Problem saving account information.");
                                break;
                            }
                        case (-2):
                            {
                                MessageBox.Show("Cannot Save: Username must be unique");
                                break;
                            }
                    } // end switch
                }
                else
                {
                    this.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmManageAccount_FormClosing(object sender, FormClosingEventArgs e)
        {
            opener.Show();
        }

        private void frmManageAccount_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible == true)
                refresh();
        }

        private void btnTitles_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmTitles titles = new frmTitles(this);
            titles.Show();
        }

        private void btnSkills_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmSkills skills = new frmSkills(this);
            skills.Show();
        }

        private void txtFirstName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtFirstName, true, false, false, false, null);
        }

        private void txtLastName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtLastName, true, false, false, false, null);
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtUserName, true, true, false, false, null);
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtUserName, true, true, false, false, null);
        }

        private void txtHomePhoneNumber_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.phoneNumber(txtHomePhoneNumber);
        }

        private void txtCellPhoneNumber_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.phoneNumber(txtCellPhoneNumber);
        }

        private void rtbAddress_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(rtbAddress, true, true, true, true, null);
        }
    }
}
